﻿using CraftParameterConfig.Infrastructure.Common;
using CraftParameterConfig.Infrastructure.Common.Enums;
using CraftParameterConfig.Services.Interfaces;
using CraftParameterConfig.Services.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CraftParameterConfig.UI.TableConfig
{
    public partial class frmTableNameConfig : Form
    {
        ITableNameService tableNameService;
        ILogInfo logInfo;
        ITableNameAuth tableNameAuth;
        UpdateTypeEnum updateType;
        public frmTableNameConfig()
        {
            InitializeComponent();
        }

        private void frmTableNameConfig_Load(object sender, EventArgs e)
        {
            tableNameService = ObjectFactory.GetObject("ITableNameService") as ITableNameService;
            logInfo = ObjectFactory.GetObject("ILogInfo") as ILogInfo;
            tableNameAuth= ObjectFactory.GetObject("ITableNameAuth") as ITableNameAuth;

            tp_Detail.PageVisible = false;
            btnSearch_Click(null, null);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确定需要取消当前操作吗?") == DialogResult.OK)
            {
                tp_Summary.PageVisible = true;
                tp_Detail.PageVisible = false;
                tc_Main.SelectedTabPage = tp_Summary;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            DataTable dtSource = tableNameService.GetTable();
            gcList.DataSource = dtSource;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (tableNameAuth.GetAddAuth("SC"))
            {
                TableNameDTO tableNameDTO = new TableNameDTO();
                txtTableName.Text = tableNameDTO.Name;
                txtTableName.Properties.ReadOnly = false;
                updateType = UpdateTypeEnum.Add;

                tp_Detail.PageVisible = true;
                tp_Summary.PageVisible = false;
                tc_Main.SelectedTabPage = tp_Detail;
            }
            else
            {
                MessageBox.Show("您没有添加表名的权限!");
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (tableNameAuth.GetUpdateAuth("SC"))
            {
                //dosomething
                //实际上当前按钮操作不需要进行任何操作,因为表名不支持修改
            }
            else
            {
                MessageBox.Show("您没有修改表名的权限!");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (tableNameAuth.GetDeleteAuth("SC"))
            {
                if (gvList.SelectedRowsCount <= 0)
                {
                    MessageBox.Show("请选择需要查询明细的表名!");
                    return;
                }
                if (MessageBox.Show("对表的删除操作可能带来严重的错误,您确定需要执行此操作吗?") == DialogResult.OK)
                {
                    string guid = gvList.GetFocusedDataRow()["GUIDID"].ToString();
                    tableNameService.DeleteTable(guid);
                }
            }
            else
            {
                MessageBox.Show("您没有删除表名的权限!");
            }
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            if (tableNameAuth.GetViewAuth("SC"))
            {
                if (gvList.SelectedRowsCount <= 0)
                {
                    MessageBox.Show("请选择需要查询明细的表名!");
                    return;
                }
                string guid = gvList.GetFocusedDataRow()["GUIDID"].ToString();
                TableNameDTO tableNameDTO = tableNameService.GetEntity(guid);
                txtTableName.Text = tableNameDTO.Name;
                txtTableName.Properties.ReadOnly = true;

                tp_Detail.PageVisible = true;
                tp_Summary.PageVisible = false;
                tc_Main.SelectedTabPage = tp_Detail;
            }
            else
            {
                MessageBox.Show("您没有查看表名的权限!");
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtTableName.Text.Trim()))
            {
                MessageBox.Show("表名不能为空!");
                return;
            }
            //判断当前数据是否已经存在
            DataTable dtSource = gcList.DataSource as DataTable;
            var filter = dtSource.Select(string.Format("[NAME]='{0}'", txtTableName.Text.Trim()));
            if (filter != null && filter.Length > 0)
            {
                MessageBox.Show("当前表名已存在相同的配置,请重新配置!");
                return;
            }
            //保存数据
            TableNameDTO tableNameDTO = new TableNameDTO();
            tableNameDTO.Name = txtTableName.Text.Trim();
            string newGuid = tableNameService.AddTable(tableNameDTO);
            //记录操作日志
            if (updateType == UpdateTypeEnum.Add)
            {
                LogDTO logDTO = new LogDTO();
                logDTO.LogType = LogType.Info;
                logDTO.KeyWord = newGuid;
                logDTO.Message = string.Format("您对表MDA_PARAMETER_NAME新增了一条数据,ID为:{0}.", newGuid);
                logInfo.SaveLog(logDTO);
                updateType = UpdateTypeEnum.None;
            }

            tp_Summary.PageVisible = true;
            tp_Detail.PageVisible = false;
            tc_Main.SelectedTabPage = tp_Summary;

            btnSearch_Click(null, null);
        }
    }
}
